package com.hcj.demo.handler;


import com.hcj.demo.util.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.form.api.*;
import org.flowable.form.engine.FormEngine;
import org.flowable.form.engine.FormEngines;
import org.flowable.form.model.FormField;
import org.flowable.form.model.SimpleFormModel;
import org.flowable.task.service.delegate.DelegateTask;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老板处理
 */
@Slf4j
public class BossTaskHandler implements TaskListener {

    private RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
    private RepositoryService repositoryService = SpringContextHolder.getBean(RepositoryService.class);

    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("delegateTask = {}", delegateTask);
        delegateTask.setDescription("此时应该由老板审批，这是来自老板的委托描述");
        // 可以设置审批截止时间
        // delegateTask.setDueDate(new Date());

        // 指定到“老板”来审批
        delegateTask.setAssignee("老板");
        delegateTask.addCandidateUser("老板");


        // 帮“老板”填写form表单, 也可在“报销单”审批的时候，自己填写审批结果
        FormEngine formEngine = FormEngines.getDefaultFormEngine();
        FormRepositoryService formRepositoryService = formEngine.getFormRepositoryService();
        FormService formService = formEngine.getFormService();

        FormDefinition form1 = formRepositoryService.createFormDefinitionQuery().formDefinitionKey("form1").singleResult();
        log.info("form1的定义 = {}", form1);
        Map<String, Object> variables = new HashMap<>();
        variables.put("input1", "这是表单中要输入的值");
        log.info("formDefinitionId = {}", form1 == null ? "" : form1.getId());
        log.info("taskId = {}", delegateTask.getId());
        log.info("processInstanceId = {}", delegateTask.getProcessInstanceId());
        log.info("processDefinitionId = {}", delegateTask.getProcessDefinitionId());
        log.info("tenantId = {}", delegateTask.getTenantId());
        log.info("outcome = {}", "同意");
        FormInstance formInstance = formService.saveFormInstanceByFormDefinitionId(variables, form1.getId(), delegateTask.getId(), delegateTask.getProcessInstanceId(), delegateTask.getProcessDefinitionId(), "", "同意");


    }

}
